/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
        List<List<Integer>>  list= new ArrayList<>();

    public List<List<Integer>> levelOrder(TreeNode root) {
        if(root==null){
            return list;
        }
        helper(root,0);
        return list;
    }

    public void helper(TreeNode root,int depth){
        if(root==null){

        }else {
            if(list.size()<=depth){ //关键，否则会报未初始化的错
                ArrayList<Integer> temp=  new ArrayList<Integer>();
                list.add(temp);
            }

            list.get(depth).add(root.val);
            if(root.left!=null){
                helper(root.left,depth+1);
            }
            if(root.right!=null){
                helper(root.right,depth+1);
            }
        }
    }
}
